// pages/classic/classic.js
import { ClassicModel } from '../../models/classic'
import { LikeModel } from '../../models/like'

let classicModel = new ClassicModel()
let likeModel = new LikeModel()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    latest: true,
    first: false,
    classic: null,
    likeCount: 0,
    likeStatus: false,
    image: '',
    content: '',
    artID: 0,
    type: 0,
    index: 0,
    pubdate: '',
    title: '',
  },

  onLike: function (event) {
    let behavior = event.detail.behavior
    likeModel.like(behavior, this.data.artID, this.data.type)
  },

  onNext: function () {
    let index = this.data.index
    classicModel.getNext(index, (res) => {
      this.updateData(res)
      this.updateIndex(this.data.index)
    })
  },

  onPrevious: function () {
    let index = this.data.index
    classicModel.getPrevious(index, (res) => {
      this.updateData(res)
      this.updateIndex(this.data.index)
    })
  },

  updateData: function (res) {
    this.setData({
      classic: res,
      image: res.image,
      likeCount:res.fav_nums,
      likeStatus:res.like_status,
      content: res.content,
      artID: res.id,
      type: res.type,
      index: res.index,
      pubdate: res.pubdate,
      title: res.title,
    })
  },

  updateIndex: function (index) {
    this.setData({
      latest: classicModel.isLatest(index),
      first: classicModel.isFirst(index),
    })
  },

  // // 应该更新缓存中likeCount、likeStatus状态
  // updateLikeStatus: function (artID, category) {
  //   likeModel.getClassicLikeStatus(artID, category, (res) => {
  //     this.setData({
  //       likeCount: res.fav_nums,
  //       likeStatus: res.like_status,
  //     })
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    classicModel.getLatest((res) => {
      this.setData({
        classic: res,
        image: res.image,
        likeCount: res.fav_nums,
        likeStatus: res.like_status,
        content: res.content,
        artID: res.id,
        type: res.type,
        index: res.index,
        pubdate: res.pubdate,
        title: res.title,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
